package org.codeblue.support.time;


import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;


/**
 * 时钟
 */
public class Clock {
    /**
     * 当前时间
     */
    public static ClockTime now() {
        return when(new Date());
    }

    public static Timestamp nowTimestamp() {
        return now().toTimestamp();
    }

    /**
     * new ClockTime()
     */
    public static ClockTime when(Date date) {
        return null == date ? NullClockTime.INSTANCE : new ClockTime(date);
    }

    public static ClockTime when(Number time) {
        return null == time ? NullClockTime.INSTANCE : when(new Date(time.longValue()));
    }

    public static ClockTime when(String value, DateFormat dateFormat) {
        return (StringUtils.isEmpty(value)) //
                ? NullClockTime.INSTANCE //
                : when(DateFormatUtil.parse(dateFormat, value));
    }

    public static ClockTime when(String value, String dateFormat) {
        return (StringUtils.isEmpty(value)) //
                ? NullClockTime.INSTANCE //
                : when(value, DateFormatPool.INSTANCE.getDateFormat(dateFormat));
    }


    /**
     * 今日指定时间
     *
     * @param hour
     * @param minute
     * @param second
     */
    public static ClockTime today(Integer hour, Integer minute, Integer second) {
        return now().setTime(hour, minute, second);
    }

    /**
     * 今天剩余的秒数
     *
     * @return
     */
    public static int todayLastSecond() {
        ClockTime now = now();
        ClockTime lastTime = now.setHour(23).setMinute(59).setSecond(59);
        return (int) ((lastTime.milliSeconds() - now.milliSeconds()) / 1000);
    }
}